package be.ipl.finito.servlets;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import be.ipl.finito.exceptions.JoueurExistantException;
import be.ipl.finito.exceptions.MotDePasseException;
import be.ipl.finito.usecases.GestionJoueur;

/**
 * Servlet implementation class Accueil
 */
public class Index extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@EJB
	private GestionJoueur gestionJoueurs;

	
	@Override
	public void init() throws ServletException {
		try {
			this.gestionJoueurs.sInscrire("el", "em", "em", "Leconte", "Emmeline", "emmeline.leconte@ipl.be");
			this.gestionJoueurs.sInscrire("md", "mi", "mi", "Debacker", "Michel", "michel.debacker@ipl.be");
			this.gestionJoueurs.sInscrire("oc", "ol", "ol", "Choquet", "Olivier", "olivier.choquet@ipl.be");

		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MotDePasseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JoueurExistantException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// Si l'utilisateur n'est pas connecte, on le dirige vers l'accueil non connecte
		if (request.getSession().getAttribute("pseudo") == null) {
			this.getServletContext().getNamedDispatcher("index.jsp").forward(request, response);
			return;
		}
		else {
			this.getServletContext().getNamedDispatcher("accueil.html").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
